package com.semp.jadoma.core.service;

import java.util.Date;
import java.util.List;

import com.semp.jadoma.core.dao.DogDAO;
import com.semp.jadoma.core.dao.FamilyReferenceDAO;
import com.semp.jadoma.core.model.Dog;
import com.semp.jadoma.core.model.FamilyReference;
import com.semp.jadoma.core.model.Dog.SexType;

public class DogService {
	
	private final static DogService instance = new DogService();
	
	private DogService() {}
	
	public static DogService getInstance() {
		return instance;
	}

	/**
	 * Find a dog
	 * @param dogId
	 * @return
	 */
	public Dog findDog(long dogId) {
		//Load Dog
		Dog dog = DogDAO.find(dogId);
		Long familyId = dog.getFamilyId();
		// Load family reference
		if (familyId != null) {
			FamilyReference familyReference = FamilyReferenceDAO.find(familyId);
			dog.setFamilyReference(familyReference);
		}
		// TODO load other stuff
		return dog;
	}
	
	/**
	 * Find a dog by name
	 * @param dogName
	 * @return
	 */
	public List findDogLikeName(String dogName) {
		return DogDAO.findLikeName(dogName);
	}
	
	/**
	 * Find a dog by name and sex
	 * @param dogName
	 * @param sexType
	 * @return
	 */
	public List findDogLikeNameAndSex(String dogName, SexType sexType) {
		return DogDAO.findLikeNameAndSex(dogName, sexType);
	}
	
	/**
	 * 
	 * @param dogName
	 * @return
	 */
	public List findDogLikeNameWithoutFamily(String dogName) {
		return DogDAO.findLikeNameWithoutFamily(dogName);
	}

	/**
	 * Add a dog to a family
	 * @param dogId
	 * @param familyId
	 */
	public void addDogToFamily(long dogId, long familyId) {
		// Load Dog
		Dog dog = DogDAO.find(dogId);
		dog.setFamilyId(familyId);
		DogDAO.update(dog);
	}
	
	public void updateDog(Dog dog) {
		DogDAO.update(dog);
	}
	
	/**
	 * Create a dog
	 * @param name
	 * @param nickName
	 * @param birthDay
	 * @param color
	 * @param size
	 * @param weight
	 * @return
	 */
	public Dog createDog(String name, String nickName, Date birthDay, int color, int peel, int sex, double size, double weight) {
		Dog dog = new Dog();
		dog.setBirthDay(birthDay);
		//dog.setBreedingId(breedingId);
		dog.setColor(color);
		dog.setPeel(peel);
		dog.setName(name);
		dog.setNickName(nickName);
		//dog.setSaleInfos(saleInfos);
		dog.setSize(size);
		dog.setWeight(weight);
		dog.setSex(sex);
		
		DogDAO.update(dog);
		
		return dog;
	}
	
}
